/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.misc;

import java.sql.*;
import java.util.*;

import nfse.NetFSE;

public class MetadataChecker {

    public static void main(String[] args) {
        try {
            if (args.length != 1) {
                System.err.println("Usage: java MetadataChecker sensor_id");
                return;
            }
            Vector entries = new Vector();
            String sql = "select * from Static_Indices where Index_Date between '2003-5-10' and '2003-5-20' and Sensor_ID="
                    + args[0];
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection connection = DriverManager.getConnection("jdbc:mysql://" + NetFSE.METADATA_HOST + "/"
                    + NetFSE.METADATA_DATABASE, NetFSE.METADATA_USER, NetFSE.METADATA_PASSWORD);
            Statement statement = connection.createStatement();
            Statement statement2 = connection.createStatement();
            Statement statement3 = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                String date = rs.getString("Index_Date");
                int sensor = rs.getInt("Sensor_ID");
                int month = rs.getInt("Month");
                int day = rs.getInt("Day");
                int year = rs.getInt("Year");
                int run = rs.getInt("Run");
                int field = rs.getInt("Field_ID");

                MetadataEntryStatus mes = new MetadataEntryStatus();
                mes.date = date;
                mes.sensor = sensor;
                mes.run = run;
                mes.month = month;
                mes.day = day;
                mes.year = year;
                boolean found = false;
                for (int i = 0; i < entries.size(); i++) {
                    MetadataEntryStatus mes2 = (MetadataEntryStatus) entries.elementAt(i);
                    if (mes2.compareTo(mes) == 0) {
                        found = true;
                        mes2.addIndexedField(field);
                        break;
                    }
                }
                if (!found) {
                    mes.addIndexedField(field);
                    entries.addElement(mes);
                    System.err.println("Added an entry: " + mes.toString());
                }
            }
            rs.close();

            sql = "select * from Data_Info where Index_Date between '2003-5-10' and '2003-5-20' and Sensor_ID="
                    + args[0];
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                String date = rs.getString("Index_Date");
                int sensor = rs.getInt("Sensor_ID");
                int month = rs.getInt("Month");
                int day = rs.getInt("Day");
                int year = rs.getInt("Year");
                int run = rs.getInt("Run");
                MetadataEntryStatus mes = new MetadataEntryStatus();
                mes.date = date;
                mes.sensor = sensor;
                mes.run = run;
                mes.month = month;
                mes.day = day;
                mes.year = year;
                mes.hasDataInfo = true;
                boolean found = false;
                for (int i = 0; i < entries.size(); i++) {
                    MetadataEntryStatus mes2 = (MetadataEntryStatus) entries.elementAt(i);
                    if (mes2.compareTo(mes) == 0) {
                        found = true;
                        mes2.hasDataInfo = true;
                        break;
                    }
                }
                if (!found) {
                    entries.addElement(mes);
                    System.err
                            .println("Added an entry for which there were no Static_Index entries: " + mes.toString());
                }
            }
            rs.close();

            MetadataEntryStatus[] sorted = new MetadataEntryStatus[entries.size()];
            for (int i = 0; i < entries.size(); i++) {
                sorted[i] = (MetadataEntryStatus) entries.elementAt(i);
            }
            Arrays.sort(sorted);

            for (int i = 0; i < sorted.length; i++) {
                MetadataEntryStatus mes = sorted[i];

            }

            for (int i = 0; i < sorted.length; i++) {
                System.out.println(sorted[i].toString());
            }

            statement3.close();
            statement2.close();
            statement.close();
            connection.close();
        } catch (Exception E) {
            E.printStackTrace();
        }
    }

}
